package Offer557;

/**
 * 反转字符串中的单词 III
 * 给定一个字符串，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
 */
public class Test {
    public static void main(String[] args) {
        String s = "Let's take LeetCode contest";
        System.out.println(new Solution().reverseWords(s));
    }
}

class Solution {
    public String reverseWords(String s) {
        String[] split = s.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String reverse = reverseString(split[i]);
            stringBuilder.append(reverse).append(" ");
        }
        return stringBuilder.toString().trim();
    }
    //这里进行每一个字符串的反转
    private String reverseString(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            stringBuilder.append(s.charAt(i));
        }
        return stringBuilder.reverse().toString();
    }
}